package com.example.schoolhelp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.schoolhelp.R;
import com.example.schoolhelp.base.BaseRecyclerAdapter;
import com.example.schoolhelp.bean.PostBean;
import com.example.schoolhelp.util.BitmapUtil;
import com.example.schoolhelp.util.ImageFromAssets;
import com.example.schoolhelp.util.MyImageAddr;
import com.example.schoolhelp.util.SystemUtil;

import java.util.ArrayList;

/**
 * Created by user on 2017/7/21.
 */

public class PostRecycleAdapter extends BaseRecyclerAdapter {
    ArrayList<PostBean> postList = new ArrayList<>();
    Context context;

    public PostRecycleAdapter(ArrayList<PostBean> postList, Context context) {
        this.postList = postList;
        this.context = context;
        addDatas(postList);
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout_post, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, Object data) {
        PostBean postItem = (PostBean) data;

        PostRecycleAdapter.MyViewHolder myViewHolder = (PostRecycleAdapter.MyViewHolder) viewHolder;
        ViewGroup.LayoutParams layoutParams = myViewHolder.frameLayout.getLayoutParams();
        layoutParams.width = SystemUtil.getScreenWidth(context) / 2 - 6;
        layoutParams.height = layoutParams.width;
        myViewHolder.frameLayout.setLayoutParams(layoutParams);

        myViewHolder.tv_author.setText(postItem.getAuthor());
        myViewHolder.tv_desc.setText(postItem.getDescription());
        myViewHolder.tv_price.setText(postItem.getPrice());

        Bitmap bitmap = ImageFromAssets.getAssetsImag(context, MyImageAddr.simpleAddr(RealPosition%10));
        myViewHolder.iv_pic.setImageBitmap(bitmap);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_author;
        ImageView iv_pic;
        TextView tv_price;
        TextView tv_desc;
        FrameLayout frameLayout;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_author = (TextView) itemView.findViewById(R.id.item_post_tv_author);
            tv_desc = (TextView) itemView.findViewById(R.id.item_post_tv_desc);
            iv_pic = (ImageView) itemView.findViewById(R.id.item_post_iv_pic);
            tv_price = (TextView) itemView.findViewById(R.id.item_post_tv_price);
            frameLayout = (FrameLayout) itemView.findViewById(R.id.item_post_flayout);
        }
    }
}
